// logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    logs: [],
    sum:[{
      title:'今日番茄次数',
      val:0
    },
    {
      title:'累计番茄次数',
      val:0
    },
    {
      title:'今日专注时长',
      val:'0分钟'
    },
    {
      title:'累计专注时长',
      val:'0分钟'
    }],
    activeIndex:0,
    dayList:[],
    list:[],
    cateArr:[
      {
        icon:"gongzuo",
        text:"工作"
      },
      {
        icon:"yuedu",
        text:"学习"
      },
      {
        icon:"sikao",
        text:"思考"
      },
      {
        icon:"xiezuo",
        text:"写作"
      },
      {
        icon:"yundong",
        text:"运动"
      },
      {
        icon:"yueduliang",
        text:"阅读"
      }
    ],
  },
  onLoad() {},
  onShow() {
    // this.setData({
    //   logs: (wx.getStorageSync('logs') || []).map(log => {
    //     return {
    //       date: util.formatTime(new Date(log)),
    //       timeStamp: log
    //     }
    //   })
    // })

    const logs = wx.getStorageSync('logs') || [];
    let day = 0,
        total = logs.length,
        dayTime = 0,
        totalTime = 0,
        dayList=[];

    if(logs.length >0){
      for(let i=0;i<logs.length;i++){
        console.log(logs[i].date)
        if(logs[i].data.toString().substr(0,10) == util.formatTime(new Date).substr(0,10)){
          //判断 时间是当天
          day = day + 1; //今日次数+1
          dayTime = dayTime + parseInt(logs[i].time); // 今日专注时长增加一次相应时长
          dayList.push(logs[i])
          this.setData({
            dayList: dayList,
            list:dayList
          })
        }
        totalTime = totalTime + parseInt(logs[i].time)
      }
      this.setData({
        'sum[0].val': day,
        'sum[1].val': total,
        'sum[2].val': dayTime+"分钟",
        'sum[3].val': totalTime+"分钟",      
      })
    }
  },
  changeType(e){
    const _idx = e.currentTarget.dataset.index
    if(_idx == 0){
      this.setData({
        list : this.data.dayList
      })
    }else if(_idx == 1){
      this.setData({
        list : wx.getStorageSync('logs') || []
      })
    }
    this.setData({
      activeIndex : _idx
    })
  }
})
